#include <stdio.h>
#include <stdlib.h>

#define LEN 10

void front(FILE * src, FILE * dst, unsigned n)
{
    char ch;
    while ((ch = fgetc(src)) != EOF && n > 0) {
        fputc(ch, dst);
        if (ch == '\n')
            --n;
    }
}

int main(int argc, char **argv)
{
    unsigned len = LEN;
    FILE * fp;

    if (argc < 2) {
        front(stdin, stdout, len);

    } else {

        while (--argc > 0) {
            if (**++argv == '-') {
                if (*++*argv == 'n')
                    len = atoi(++*argv);
                else {
                    fputs("参数错误!\n", stderr);
                    return 1;
                }

            } else {

                if ((fp = fopen(*argv, "r")) != NULL) {
                    front(fp, stdout, len);
                    fclose(fp);

                } else {
                    fprintf(stderr, "文件%s不存在!\n", *argv);
                    return 1;
                }
            }
        }
    }

    return 0;
}
